using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using S2Engine.Physics;
using S2Engine.Game;

namespace S2Engine.Spriting
{
    public class Sprite : IVisible2D, IMovable2D
    {
        private Texture2D texture;
        private int currentFrameIndex = -1;
        private S2Game Game;
        private Color color = new Color(new Vector4(1f, 1f, 1f, 1f));
        private float rotation;
        private Vector2 origin;
        private Vector2 position;
        private float opacity = 1f;
        private bool visible = true;
        private Frame currentFrame;
        private Vector2 velocity;
        private int currentDelay = -1;
        private bool loop;
        private int firstFrame;
        private int lastFrame;
        private bool paused;

        private Frame[] frames;

        public Sprite(S2Game game, string texture, Frame[] frames, bool loop)
        {
            this.Game = game;
            this.texture = Texture2D.FromFile(Game.Graphics.GraphicsDevice,texture);
            this.frames = frames;
            firstFrame = 0;
            lastFrame = frames.Length - 1;
            this.loop = loop;
            this.currentFrameIndex = 0;
        }

        public Sprite(S2Game game, Texture2D texture, Frame[] frames, bool loop)
        {
            this.Game = game;
            this.texture = texture;
            this.frames = frames;
            firstFrame = 0;
            lastFrame = frames.Length - 1;
            this.loop = loop;
            this.currentFrameIndex = 0;
        }

        public Sprite(S2Game game, Texture2D texture, Frame[] frames, int firstFrame, bool loop)
        {
            this.Game = game;
            this.texture = texture;
            this.frames = frames;
            firstFrame = 0;
            lastFrame = frames.Length - 1;
            this.loop = loop;
            this.currentFrameIndex = firstFrame;
        }

        public bool Loop
        {
            get { return loop; }
            set { loop = value; }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = true; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public float Opacity
        {
            get { return opacity; }
            set { opacity = value; }
        }
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public float Rotation
        {
            set { rotation = value; }
            get { return rotation; }
        }
        public int Width
        {
            get { return frames[currentFrameIndex].TextureArea.Width; }
        }

        public int Height
        {
            get { return frames[currentFrameIndex].TextureArea.Height; }
        }

        public Frame CurrentFrame
        {
            get { return currentFrame; }
        }

        public int CurrentFrameIndex
        {
            get { return currentFrameIndex; }
        }

        public Texture2D Texture
        {
            get { return texture; }
        }

        public bool Paused
        {
            get { return paused; }
        }

        public void Play()
        {
            paused = false;
        }

        public void Pause()
        {
            paused = true;
        }
        public virtual void Update(GameTime gameTime)
        {
            if (paused)
                return;
            currentDelay--;
            if (currentDelay < 0)
            {
                currentFrameIndex++;
                if (currentFrameIndex > lastFrame)
                {
                    if (loop)
                        currentFrameIndex = firstFrame;
                    else
                        currentFrameIndex = lastFrame;
                }
                currentDelay = frames[currentFrameIndex].Delay;
                currentFrame = frames[currentFrameIndex];
                origin = (new Vector2(currentFrame.TextureArea.Width, currentFrame.TextureArea.Height)) / 2;
            }

            position = new Vector2(position.X + (velocity.X * gameTime.ElapsedGameTime.Milliseconds),
                                   position.Y + (velocity.Y * gameTime.ElapsedGameTime.Milliseconds));
        }

        public virtual void Draw()
        {
            if (CurrentFrame != null)
            {
                // Batch.Draw(texture, new Vector2(currentFrame * width , startOffset), color);

                //if (opacity < 1)
                color = new Color(new Vector4(1f, 1f, 1f, opacity));

                if (rotation == 0)
                    Game.Batch.Draw(texture, position,
                        currentFrame.TextureArea,
                        color);
                else
                    Game.Batch.Draw(texture,
                        position,
                        currentFrame.TextureArea,
                        color,
                        rotation,
                        origin,
                        1f,
                        SpriteEffects.None,
                        0f);
            }
        }

        public bool CollidingSquare(Sprite sprite)
        {
            //return this.positionX < sprite.Width && this.width > sprite.PositionX && PositionY < sprite.Height && Height > sprite.PositionY;
            if (((sprite.position.X < (this.position.X + this.Width)) && (this.position.X < (sprite.position.X + sprite.Width))) && (sprite.Position.Y < (this.Position.Y + this.Height)))
            {
                return (this.Position.Y < (sprite.Position.Y + sprite.Height));
            }
            return false;

            
        }
        public virtual void End()
        {
            currentFrameIndex = 0;
        }

        

  
    
    }
}
